//
//  LRHomeHeardCell.m
//  本地生活圈
//
//  Created by admin on 15/8/12.
//  Copyright (c) 2015年 懒人. All rights reserved.
//

#import "LRHomeHeardCell.h"

@interface LRHomeHeardCell ()<UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UILabel *titleLable;
@property (weak, nonatomic) IBOutlet UIPageControl *pageControl;
/**
 *  时钟
 */
@property (nonatomic, strong) NSTimer *timer;

@end
@implementation LRHomeHeardCell


- (void)setFocus:(LRFocusModel *)focus
{
    _focus = focus;
    
    // 设置分页
    self.pageControl.numberOfPages = focus.list.count;
    self.pageControl.currentPage = 0;
    self.pageControl.currentPageIndicatorTintColor = [UIColor redColor];
    self.pageControl.pageIndicatorTintColor = [UIColor grayColor];
    [self.pageControl addTarget:self action:@selector(pageChange) forControlEvents:UIControlEventTouchUpInside];
    self.titleLable.textColor = [UIColor whiteColor];
    // 设置scrollView
    CGRect rect = self.bounds;
    self.scrollView.contentSize = CGSizeMake(self.pageControl.numberOfPages * rect.size.width, 0);
    // 设置cell

    for (int i = 0; i < self.pageControl.numberOfPages; i ++)
    {
        LRFocusListModel *list = focus.list[i];
        UIImageView *imageView = [[UIImageView alloc]init];
        [imageView sd_setImageWithURL:[NSURL URLWithString:list.cover] placeholderImage:[UIImage imageNamed:@"placeholder"]];
        CGRect frame = CGRectMake(rect.size.width * i, 0, rect.size.width, rect.size.height);
        imageView.frame = frame;
        [self.scrollView addSubview:imageView];
    }

    // 启动时钟
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updatePage) userInfo:nil repeats:YES];
}

// 页面控制器监控事件
- (void)pageChange
{
    // 根据当前页数设置scrollView的offset位置
    NSInteger currentPage = self.pageControl.currentPage;
    CGFloat x = self.bounds.size.width * currentPage;
    self.scrollView.contentOffset = CGPointMake(x, 0);
    
    // 设置title
    self.titleLable.text = [self.focus.list[currentPage] title];
}

// 实现时钟监控事件
- (void)updatePage
{
    NSInteger currentPage = self.pageControl.currentPage;
    if (currentPage == self.pageControl.numberOfPages - 1)
    {
        self.pageControl.currentPage = 0;
    }
    else
        self.pageControl.currentPage ++;
    
    [self pageChange];
}

#pragma mark - UIScrollViewDelegate
// 拖拽时
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    self.pageControl.currentPage = self.scrollView.contentOffset.x / self.scrollView.contentSize.width;
}

- (void)awakeFromNib {
    // Initialization code
    
    // 初始化
    self.scrollView.backgroundColor = [UIColor whiteColor];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
